//
//  UIControl+DoubleClick.m
//  Passenger
//
//  Created by suncong on 2017/4/9.
//  Copyright © 2017年 chuangxing-1. All rights reserved.
//

#import "UIControl+DoubleClick.h"
#import <objc/runtime.h>

@implementation UIControl (DoubleClick)

static const char *UIControl_acceptEventInterval = "UIControl_acceptEventInterval";

- (NSTimeInterval)cs_acceptEventInterval {
    return  [objc_getAssociatedObject(self, UIControl_acceptEventInterval) doubleValue];
}

- (void)setCs_acceptEventInterval:(NSTimeInterval)cs_acceptEventInterval {
    objc_setAssociatedObject(self, UIControl_acceptEventInterval, @(cs_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

static const char *UIControl_acceptEventTime = "UIControl_acceptEventTime";

- (NSTimeInterval)cs_acceptEventTime {
    return  [objc_getAssociatedObject(self, UIControl_acceptEventTime) doubleValue];
}

- (void)setCs_acceptEventTime:(NSTimeInterval)cs_acceptEventTime {
    objc_setAssociatedObject(self, UIControl_acceptEventTime, @(cs_acceptEventTime), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

// 在load时执行hook
+ (void)load {
    Method before   = class_getInstanceMethod(self, @selector(sendAction:to:forEvent:));
    Method after    = class_getInstanceMethod(self, @selector(cs_sendAction:to:forEvent:));
    method_exchangeImplementations(before, after);
}

- (void)cs_sendAction:(SEL)action to:(id)target forEvent:(UIEvent *)event {
    if ([NSDate date].timeIntervalSince1970 - self.cs_acceptEventTime < self.cs_acceptEventInterval) {
        return;
    }
    
    if (self.cs_acceptEventInterval > 0) {
        self.cs_acceptEventTime = [NSDate date].timeIntervalSince1970;
    }
    
    [self cs_sendAction:action to:target forEvent:event];
}

@end

@implementation UITableViewCell (DoubleSelect)

static const char *UITableViewCell_acceptEventInterval = "UITableViewCell_acceptEventInterval";

- (NSTimeInterval)tbc_acceptEventInterval {
    return  [objc_getAssociatedObject(self, UITableViewCell_acceptEventInterval) doubleValue];
}

- (void)setTbc_acceptEventInterval:(NSTimeInterval)tbc_acceptEventInterval {
    objc_setAssociatedObject(self, UITableViewCell_acceptEventInterval, @(tbc_acceptEventInterval), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

static const char *UITableViewCell_acceptEventTime = "UITableViewCell_acceptEventTime";

- (NSTimeInterval)tbc_acceptEventTime {
    return  [objc_getAssociatedObject(self, UITableViewCell_acceptEventTime) doubleValue];
}

- (void)setTbc_acceptEventTime:(NSTimeInterval)tbc_acceptEventTime {
    objc_setAssociatedObject(self, UITableViewCell_acceptEventTime, @(tbc_acceptEventTime), OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

static const char *UITableView_ = "UITableView";

- (UITableView *)tableView {
    return  objc_getAssociatedObject(self, UITableView_);
}

- (void)setTableView:(UITableView *)tableView
{
    objc_setAssociatedObject(self, UITableView_, tableView, OBJC_ASSOCIATION_ASSIGN);
}

// 在load时执行hook
+ (void)load {
    Method before   = class_getInstanceMethod(self, @selector(setSelected:animated:));
    Method after    = class_getInstanceMethod(self, @selector(tbc_setSelected:animated:));
    method_exchangeImplementations(before, after);
}

- (void)tbc_setSelected:(BOOL)selected animated:(BOOL)animated
{
    if (self.tbc_acceptEventInterval > 0) {
        self.tbc_acceptEventTime = [NSDate date].timeIntervalSince1970;
        self.tableView.allowsSelection = NO;
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(self.tbc_acceptEventInterval * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.tableView.allowsSelection = YES;
        });
    }
    
    [self tbc_setSelected:selected animated:animated];
}

@end

